<template>
  <div>
    <Carousel></Carousel>

    <!--    名片-->
    <el-card class="box-card">
      <div slot="header">
        <span>我的名片</span>
      </div>
      <div v-for="(item,index) in cardInformation" class="text">
        <span class="key">{{index+':'}}</span>
        <span>{{item}}</span>
      </div>
    </el-card>

    <div class="blogContainerLeft">
      <!--    博客内容区域-->
      <ul class="blogItems">
        <li v-for="blog in tableData" :key="blog._id">
          <div class="blogTitle">
            {{blog.title}}
            <span @click="jump(blog._id)">阅读全文</span>
          </div>
          <div class="blogContainer">
            <div class="blogImg">
              <img :src="blog.imgUrl" alt="加载失败">
            </div>
            <div class="blogText">
              {{blog.text}}
            </div>
          </div>
          <div class="blogFooter">
            <div class="left">{{parseFloat(blog.date) | dateFormat}}</div>
            <div class="right">
              <span @click="fabulous(blog)" class="iconfont icon-zan">&nbsp;{{blog.top}}</span>
              <span @click="step(blog)" class="iconfont icon-cai">&nbsp;{{blog.trample}}</span>
            </div>
          </div>
          <div class="deleteBlog" @click="deleteBlog(blog._id)" v-if="jurisdiction">×</div>
        </li>
      </ul>
      <!--      分页区域-->
      <el-pagination
              v-if="this.$store.state.blogs"
              @current-change="handleCurrentChange"
              :current-page="pagination.currentPage"
              :page-size="pagination.pageSize"
              layout="prev, pager, next"
              :total="pagination.total"
              class="pagination">
      </el-pagination>
    </div>


    <!--    右侧-->
    <div class="blogContainerRight">
      <!--      搜索-->
      <Search @init="init" @getBlogs="getBlogs"></Search>

      <!--    日历-->
      <Calendar></Calendar>

      <!--      音乐-->
      <Music></Music>
    </div>

  </div>
</template>

<script>
  import Carousel from "../components/Carousel";
  import Calendar from "../components/Calendar";
  import Music from "../components/Music";
  import Search from "../components/Search";
  import {mapState, mapGetters} from 'vuex'

  export default {
    name: "blog",
    components: {
      Carousel,
      Calendar,
      Music,
      Search
    },
    data() {
      return {
        cardInformation: {
          '昵称': 'empty',
          QQ: '1573959254',
          '微信': 'cdy123666123',
          '邮箱': '1573959254@qq.com'
        },  //名片信息
        pagination: {
          pageSize: 3,
          currentPage: 1,
          total: this.$store.state.blogs.length
        },
        tableData: []
      }
    },
    methods: {
      init(){
        this.pagination.total = this.$store.state.blogs.length
        this.pagination.currentPage = 1
        this.pagination.pageSize = 3
        //设置默认分页数据
        this.tableData = this.$store.state.blogs.filter((item, index) => {
          return index < this.pagination.pageSize
        })
      },
      getBlogs() {
        this.$store.dispatch('getBlogs').then(() => {
          this.init()
        })
      },  //获取博客
      jump(id) {
        this.$store.state.blogId = id
        this.$router.push('/read')
      },  //跳转博客详情页面
      deleteBlog(id) {
        this.$store.dispatch('removeBlog', id)
        this.getBlogs()
      },  //删除博客
      handleCurrentChange(page) {
        //获取当前页  头
        let index = this.pagination.pageSize * (page - 1)

        //数据总数
        let nums = this.pagination.pageSize * page
        //容器
        let tables = []
        for (let i = index; i < nums; i++) {
          if (this.$store.state.blogs[i]) {
            tables.push(this.$store.state.blogs[i])
          }
          this.tableData = tables
        }
      },
      fabulous(blog) {
        if (window.sessionStorage.getItem('id') === blog._id) {
          window.sessionStorage.removeItem('id')
          blog.top--
        } else {
          window.sessionStorage.setItem('id', blog._id)
          blog.top++
        }
        this.$store.dispatch('fabulous', {id: blog._id, top: blog.top})
      },  //赞
      step(blog) {
        if (window.sessionStorage.getItem('ids') === blog._id) {
          window.sessionStorage.removeItem('ids')
          blog.trample--
        } else {
          window.sessionStorage.setItem('ids', blog._id)
          blog.trample++
        }
        this.$store.dispatch('step', {id: blog._id, trample: blog.trample})
      },  //踩
    },
    created() {
      this.getBlogs()
    },
    computed: {
      ...mapState(['blogs']),
      ...mapGetters(['jurisdiction'])
    }
  }
</script>

<style scoped lang="less">
  .box-card {
    width: 300px;
    height: 150px;
    display: inline-block;
    margin-left: 40px;
    background: #000;
    box-shadow: 0 -1px 2px #000 inset;
    border: none;
    color: #fff;
    opacity: .4;

    .text {
      margin: 8px 0;

      .key {
        width: 60px;
        display: inline-block;
        text-align: right;
        margin-right: 20px;
      }
    }
  }

  .blogItems {
    width: 600px;
    margin-top: 40px;
    display: inline-block;

    li {
      width: 100%;
      border-radius: 5px;
      overflow: hidden;
      margin-top: 10px;
      box-shadow: 0 1px 1px #3D3D3D,0 -1px 1px #000;
      position: relative;

      .blogTitle {
        width: 100%;
        line-height: 30px;
        background: #222222;
        padding: 5px 0 5px 20px;
        font-size: 16px;
      }

      .blogContainer {
        width: 100%;
        display: flex;
        background: #222222;
        padding: 5px 20px 20px 20px;

        .blogImg {
          flex: 1;
          width: 100%;

          img {
            width: 100%
          }
        }

        .blogText {
          flex: 4;
          height: 100%;
          font-size: 12px;
          margin: 0 0 0 20px;
          line-height: 16px;
          text-indent: 2em;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 4;
          -webkit-box-orient: vertical;
        }
      }

      .blogFooter {
        width: 100%;
        background: #181818;
        overflow: hidden;
        padding: 10px;

        .left {
          float: left;
        }

        .right {
          float: right;

          span {
            margin-right: 10px;
            vertical-align: middle;
            cursor: pointer;
          }
        }
      }

      .deleteBlog {
        width: 15px;
        height: 15px;
        border-radius: 50%;
        background: #000;
        line-height: 15px;
        text-align: center;
        position: absolute;
        top: 0px;
        left: 0px;
        z-index: 10;
        color: white;
        cursor: pointer;
      }
    }
  }

  .blogContainerRight, .blogContainerLeft {
    float: left;
  }

  .blogContainerRight {
    margin: 50px 0 0 40px;
  }

  .blogTitle {
    span {
      color: #808080;
      font-size: 12px;
      float: right;
      margin-right: 10px;
      text-decoration: underline;
      cursor: pointer;
    }

    span:hover {
      color: #00bcd4;
    }
  }

  .pagination {
    margin-top: 10px;
    text-align: right;
  }

</style>
